package com.luying.service.impl;

import com.luying.dao.UserDao;
import com.luying.pojo.ChangePassword;
import com.luying.pojo.MenuList;
import com.luying.pojo.User;
import com.luying.service.AuthService;
import com.luying.utils.JsonUtils;
import com.luying.utils.MD5Utils;
import com.luying.utils.jedis.JedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuthServiceImpl implements AuthService {
    private Logger logger = LoggerFactory.getLogger(AuthServiceImpl.class);
    @Autowired
    private UserDao userDao;
    @Autowired
    private JedisClient jedisClient;
    @Override
    public MenuList login(String account, String password) {
        MenuList menuList = new MenuList();
        String s = MD5Utils.md5Password(password);
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("account",account).andEqualTo("password",s);
        List<User> users = userDao.selectByExample(example);
        if(users!=null&&users.size()>0){
            User user = users.get(0);
            menuList = userDao.selectMenus(user.getId());
            menuList.setUser(user);
            //操作成功插入redis
            String key = user.getId().toString();
            if(StringUtils.isEmpty(jedisClient.get(key))){
                jedisClient.set(key, JsonUtils.beanToJson(menuList));
            }
            return menuList;
        }
        return menuList;
    }

    @Override
    public MenuList getMe(Integer id) {
        Map<String,Object> resp = new HashMap<>(16);
        //操作成功插入redis
        String s = jedisClient.get(id.toString());
        MenuList menuList= (MenuList) JsonUtils.jsonToBean(s, MenuList.class);
        return menuList;
    }

    @Override
    public Map<String, Object> logout(Integer id) {
        jedisClient.del(id.toString());
        return null;
    }

    @Override
    public void changePassword(ChangePassword changePassword) throws Exception {
        //获取人员信息
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("id",changePassword.getId());
        List<User> users = userDao.selectByExample(example);
        User user = users.get(0);
        String password = MD5Utils.md5Password(changePassword.getOldPassword());
        if(!password.equals(user.getPassword())){
            throw new Exception("原密码输入错误!");
        }else {
            if(!changePassword.getOneNewPassword().equals(changePassword.getTwoNewPassword())){
                throw new Exception("二次密码输入不一致,请重新输入!");
            }else {
                user.setPassword(MD5Utils.md5Password(changePassword.getOneNewPassword()));
                userDao.updateByExampleSelective(user,example);
            }
        }
    }

    @Override
    public int resetPassword(String account) {
        String s = MD5Utils.md5Password("123456");
        return userDao.updatePasswordByAccount(s,account);
    }
}
